package com.example.practice.view;

import android.graphics.Color;
import android.icu.text.CaseMap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.fragment.app.FragmentActivity;

import com.bumptech.glide.Glide;
import com.example.practice.R;
import com.example.practice.activity.registerActivity_yl;
import com.example.practice.adapter.CourseAdapter;
import com.example.practice.bean.CourseBean;
import com.example.practice.bean.IMGBean;
import com.example.practice.bean.ResultBean;
import com.example.practice.utils.OkHttpUtils;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class CourseView {
    private FragmentActivity mContext;
    private LayoutInflater inflater;
    private View currentView;
    private Banner banner;
    private GridView lv_list;
    private List<IMGBean> list;
    private List<CourseBean> courseList;
    private CourseAdapter adapter;

    public CourseView(FragmentActivity mContext) {
        this.mContext = mContext;
        inflater=LayoutInflater.from(mContext);
    }

    public void initView(){
        currentView=inflater.inflate(R.layout.course_view,null);

        banner=currentView.findViewById(R.id.banner);
        banner.setAdapter(new BannerImageAdapter<IMGBean>(list) {
            @Override
            public void onBindView(BannerImageHolder holder, IMGBean data, int position, int size) {
                Glide.with(holder.imageView)
                        .load(data.imageRes)
                        .into(holder.imageView);
            }
        })
                .isAutoLoop(true)
                .addBannerLifecycleObserver(mContext)
                .setIndicator(new CircleIndicator(mContext));

        lv_list=currentView.findViewById(R.id.gird_view);
        adapter = new CourseAdapter(mContext, courseList);
        lv_list.setAdapter(adapter);
    }

    private void createView() {
        getCourseData();
        initData();
        initView();
    }

    private void getCourseData() {
        courseList=new ArrayList<>();
        OkHttpUtils.sendOkHttp(true, true, "androidCourse/GetList?kfz=jim&deptID=110", null, new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {
                        mContext.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(mContext,"网络连接异常",Toast.LENGTH_SHORT).show();
                            }
                        });
                    }

                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                        try {

                            JSONArray jsonArray = new JSONArray(response.body().string());
//                            Log.i("res", ""+jsonArray.optJSONObject(0));
                            mContext.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    for (int i = 0; i < jsonArray.length(); i++) {
                                        JSONObject res = jsonArray.optJSONObject(i);
//                                        Log.i("res", ""+res.optString("Title"));
                                        courseList.add(new CourseBean(res.optInt("id"),
                                                res.optString("ImgTitle"), res.optString("Title"),
                                                res.optString("Intro")));
                                    }
                                }
                            });
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    public View getView(){
        if (currentView == null){
            createView();
        }
        return currentView;
    }

    public void showView(){
        if (currentView == null){
            createView();
        }
        currentView.setVisibility(View.VISIBLE);
    }

    private void initData() {
        list = new ArrayList<>();
        list.add(new IMGBean(R.drawable.banner_1));
        list.add(new IMGBean(R.drawable.banner_2));
        list.add(new IMGBean(R.drawable.banner_3));
    }
}
